package www.openteam.com.ui.prdctnMngmnt;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.v7.widget.AppCompatEditText;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import www.openteam.com.R;
import www.openteam.com.adapter.PrdctnMngmnt.SRCKPlanAdp;
import www.openteam.com.adapter.PrdctnMngmnt.SRCKPoduAdp;
import www.openteam.com.base.mBaseFragment;
import www.openteam.com.bean.MoneyType;
import www.openteam.com.bean.OutFactoryMsg;
import www.openteam.com.bean.PayType;
import www.openteam.com.bean.SRCKPlanMsg;
import www.openteam.com.bean.SRCPoduMsg;
import www.openteam.com.contract.PrdctnMngmnt.SendRCKContract;
import www.openteam.com.presenter.prdctnMngmnt.SendRCKPrst;
import www.openteam.com.tool.ToastUtil;


public class SendRCheckFragment extends mBaseFragment<SendRCKPrst> implements SendRCKContract.SendRCKView {

    @Bind(R.id.SRC_outsourceF)
    TextView SRCOutsourceF;
    @Bind(R.id.SRC_date)
    TextView SRCDate;
    @Bind(R.id.SRC_consignee)
    Spinner SRCConsignee;
    @Bind(R.id.SRC_recydate)
    TextView SRCRecydate;
    @Bind(R.id.SRC_phone)
    AppCompatEditText SRCPhone;
    @Bind(R.id.SRC_fax)
    AppCompatEditText SRCFax;
    @Bind(R.id.SRC_currency)
    Spinner SRCCurrency;
    @Bind(R.id.SRC_paymethon)
    Spinner SRCPaymethon;
    @Bind(R.id.SRC_sendRType)
    Spinner SRCSendRType;
    @Bind(R.id.SRC_taxRate)
    AppCompatEditText SRCTaxRate;
    @Bind(R.id.SRC_recyAdress)
    AppCompatEditText SRCRecyAdress;
    @Bind(R.id.SRC_reamke)
    AppCompatEditText SRCReamke;
    onSendRCheckSearch sendRCheckSearch;
    @Bind(R.id.NSRC_outsourceF)
    Button BSRCOutsourceF;
    @Bind(R.id.SRC_query_btn)
    Button SRCQueryBtn;
    @Bind(R.id.SRC_recylceView)
    RecyclerView SRCrecyleView;
    @Bind(R.id.SRC_searchType)
    Spinner SRCsearchType;
    private String TAG = getClass().getSimpleName();
    private SRCKPoduAdp srckPoduAdp;
    private SRCKPlanAdp srckPlanAdp;
    private ArrayList<String> newTypeList;
    SendRCKClickListener sendRCKClickListener;

    public void setSendRCheckSearch(onSendRCheckSearch sendRCheckSearch) {
        this.sendRCheckSearch = sendRCheckSearch;
    }


    public SendRCheckFragment() {
        // Required empty public constructor
    }

    public interface SendRCKClickListener{
        void postPoduData(SRCPoduMsg.DataBeanX.DataBean dataBean,String tag);
        void postPlanData(SRCKPlanMsg.DataBeanX.DataBean dataBean,String tag);
    }

    public void setSendRCKClickListener(SendRCKClickListener sendRCKClickListener) {
        this.sendRCKClickListener = sendRCKClickListener;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_send_rcheck, container, false);
        ButterKnife.bind(this, view);
        mPresenter = loadPresenter();
        initData();
        initListener();
        initView();
        return view;
    }

    public void getdata(OutFactoryMsg.DataBeanX.DataBean dataBean) {
        SRCOutsourceF.setText(dataBean.getCode());
        SRCPhone.setText(dataBean.getTel());
        SRCFax.setText(dataBean.getFax());
        Calendar ca = Calendar.getInstance();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SRCDate.setText(dateFormat.format(ca.getTime()));
    }

    @Override
    protected void initData() {
        if (mPresenter != null)
            mPresenter.attachView(this);
        if (mProgressDialog == null) {
            mProgressDialog = new ProgressDialog(getContext());
        }
        ArrayList<String> sendTypeList = new ArrayList<>();
        sendTypeList.add("按工程单");
        sendTypeList.add("按发外计划");
        ArrayAdapter<String> sendTypeAdapter = new ArrayAdapter<String>(getContext(),
                android.R.layout.simple_spinner_dropdown_item, sendTypeList);
        sendTypeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        SRCSendRType.setAdapter(sendTypeAdapter);

        newTypeList = new ArrayList<>();
        newTypeList.add("发外加工明细");
        newTypeList.add("发外加工辅料");
        newTypeList.add("发外加工纸张");
        newTypeList.add("发外加工坑纸");
        ArrayAdapter<String> newTypeAdapter = new ArrayAdapter<String>(getContext(),
                android.R.layout.simple_spinner_dropdown_item, newTypeList);
        newTypeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        SRCsearchType.setAdapter(newTypeAdapter);

        SRCrecyleView.setLayoutManager(new LinearLayoutManager(getContext()));


    }

    @Override
    protected void initListener() {
        BSRCOutsourceF.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPresenter.findMoneyType();
                mPresenter.findPayType();
                if (sendRCheckSearch != null) {
                    sendRCheckSearch.OnOutFatoryMsg();
                }
            }
        });
        SRCRecydate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                selectCalendar(SRCRecydate);
            }
        });
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void otherViewClick(View view) {

    }

    @Override
    protected SendRCKPrst loadPresenter() {
        return new SendRCKPrst();
    }


    @Override
    public void onRefresh() {

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (mPresenter != null)
            mPresenter.detachView();
        ButterKnife.unbind(this);
    }

    @Override
    public void findMoneryType(MoneyType moneyType) {
        ArrayList<String> moneyTypeList = new ArrayList<>();
        List<MoneyType.DataBeanX.DataBean> data = moneyType.getData().getData();
        for (MoneyType.DataBeanX.DataBean dataBean : data) {
            moneyTypeList.add(dataBean.getMoneytp());
        }
        Log.e(TAG, "findMoneryType: " + moneyTypeList.size());
        ArrayAdapter<String> moneyTypeAdapter = new ArrayAdapter<String>(getContext(),
                android.R.layout.simple_spinner_dropdown_item, moneyTypeList);
        moneyTypeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        SRCCurrency.setAdapter(moneyTypeAdapter);
//        if (sendRCheckSearch != null) {
//            sendRCheckSearch.OnOutFatoryMsg();
//        }
    }

    @Override
    public void findMoneryTypeFail(String msg) {
        ToastUtil.showToast(getContext(), msg);
    }

    @Override
    public void findPayType(PayType payType) {
        ArrayList<String> payTypeList = new ArrayList<>();
        List<PayType.DataBeanX.DataBean> data = payType.getData().getData();
        for (PayType.DataBeanX.DataBean dataBean : data) {
            payTypeList.add(dataBean.getName());
        }
        ArrayAdapter<String> payTypeAdapter = new ArrayAdapter<String>(getContext(),
                android.R.layout.simple_spinner_dropdown_item, payTypeList);
        payTypeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        SRCPaymethon.setAdapter(payTypeAdapter);
    }

    @Override
    public void findPayTypeFail(String msg) {
        ToastUtil.showToast(getContext(), msg);
    }

    @Override
    public void findPoduMsg(SRCPoduMsg srcPoduMsg) {
        mProgressDialog.cancel();
        if (null == srckPoduAdp) {
            srckPoduAdp = new SRCKPoduAdp(getContext(), srcPoduMsg.getData().getData());
        }
        SRCrecyleView.setAdapter(srckPoduAdp);
        srckPoduAdp.setSrckPoduClickBack(new SRCKPoduAdp.SRCKPoduClickBack() {
            @Override
            public void postdata(SRCPoduMsg.DataBeanX.DataBean dataBean) {
                if (sendRCKClickListener!=null){
                    sendRCKClickListener.postPoduData(dataBean,"podu");
                }
            }
        });


    }

    @Override
    public void findPoduMsgFail(String msg) {
        mProgressDialog.cancel();
        ToastUtil.showToast(getContext(), msg);
    }

    @Override
    public void findPlanMsg(final SRCKPlanMsg srckPlanMsg) {
        mProgressDialog.cancel();
        if (null == srckPlanAdp) {
            srckPlanAdp = new SRCKPlanAdp(getContext(), srckPlanMsg.getData().getData());
        }
        SRCrecyleView.setAdapter(srckPlanAdp);
        srckPlanAdp.setSrckPlanClickBack(new SRCKPlanAdp.SRCKPlanClickBack() {
            @Override
            public void postdata(SRCKPlanMsg.DataBeanX.DataBean dataBean) {
                if (sendRCKClickListener!=null){
                    sendRCKClickListener.postPlanData(dataBean,"plan");
                }
            }
        });
    }

    @Override
    public void findPlanMsgFail(String msg) {
        mProgressDialog.cancel();
        ToastUtil.showToast(getContext(), msg);
    }

    @OnClick(R.id.SRC_query_btn)
    public void onViewClicked() {
        String s = SRCSendRType.getSelectedItem().toString();//发外类型
        String s1= SRCsearchType.getSelectedItem().toString();
        if (s1.contains("发外加工明细")){
            if (s.contains("按工程单")){
                mPresenter.findPoduMsg();
                mProgressDialog.show();
            }else if (s.contains("按发外计划")){
                if (SRCOutsourceF.getText().toString().isEmpty()){
                    ToastUtil.showToast(getContext(),"外协厂编号不能为空");
                }else {
                    mPresenter.findPlanMsg(SRCOutsourceF.getText().toString());
                    mProgressDialog.show();
                }
            }
        }else if (s1.contains("发外加工辅料")){

        }else if (s1.contains("发外加工纸张")){

        }else if (s1.contains("发外加工坑纸")){

        }

//        SRCsearchType.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
//            @Override
//            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//                String s = newTypeList.get(position);
//                if (s.contains("发外加工明显")){
//                    mPresenter.findPoduMsg();
//                }else if (s.contains("发外加工辅料")){
//
//                }else if (s.contains("发外加工纸张")){
//
//                }else if (s.contains("发外加工坑纸")){
//
//                }
//            }
//
//            @Override
//            public void onNothingSelected(AdapterView<?> parent) {
//
//            }
//        });

    }


    public interface onSendRCheckSearch {
        void OnOutFatoryMsg();
    }

}
